package com.irdstudio.apicenter.gateway.core.token;

import java.util.Map;

import com.irdstudio.apicenter.gateway.core.jdbc.MaccsBaseDAO;
import com.irdstudio.apicenter.gateway.util.spring.BeanUtils;

/**
 * 会话令牌管理（相当于会话，支持分布式部署)
 * 
 * @author guangming.li
 * @version 1.0
 * @date 2016-01-12
 */
public class AccessTokenUtil {
	private static final String queryTokenIdSql = "select * from APP_DEVICE_REGISTER where token_id=?";

	/**
	 * 获取Token,根据传入的token，从数据库中实时读出
	 * 
	 * @param tokenId
	 * @return
	 */
	public final static AccessToken getAccessToken(String tokenId) {
		// 获取数据库权限
		MaccsBaseDAO jdbcDao = BeanUtils.getBean(MaccsBaseDAO.class);
		Map<String, Object> tokenMap = jdbcDao.queryForMap(queryTokenIdSql, new Object[] { tokenId });
		// 保存进AccessToken对象中
		if (tokenMap != null && tokenMap.size() != 0) {
			AccessToken accessToken =new AccessToken();
			accessToken.setAppId(tokenMap.get("app_id").toString());
			accessToken.setAddress(tokenMap.get("address").toString());
			accessToken.setAppUserid(tokenMap.get("app_userid").toString());
			accessToken.setClientIp(tokenMap.get("client_ip").toString());
			accessToken.setDeviceId(tokenMap.get("device_id").toString());
			accessToken.setDeviceModel(tokenMap.get("device_model").toString());
			accessToken.setDeviceType(tokenMap.get("device_type").toString());
			accessToken.setLastTokenId((String)tokenMap.get("last_token_id"));
			accessToken.setMobilePhone(tokenMap.get("mobile_phone").toString());
			accessToken.setTokenId(tokenMap.get("token_id").toString());
			accessToken.setTokenTime(tokenMap.get("token_time").toString());
			return accessToken;
		}{
			System.out.println("数据库数据异常");
		}
		return null;
	}

	/**
	 * 将Token放入到MAP中进行缓存(暂不使用)
	 * 
	 * @param token
	 */
	public final static void pushAccessToken(AccessToken token) {

	}

}
